from mxupy import IM, EntityXControl
import OBEvolutionAI as obe


class DeptControl(EntityXControl):

    class Meta:
        model_class = obe.Dept

    def get_list(self, select=None, where=None, order_by=None, group_by=None, having=None, limit=100, offset=None, to_dict=False, recurse=False, backrefs=False, max_depth=1):
        """ 获取系列表 """
        return super().get_list(select=select, where=where, order_by=order_by, group_by=group_by, having=having, limit=limit, offset=offset, to_dict=to_dict, recurse=recurse, backrefs=backrefs, max_depth=max_depth)
    
        # 删除系
    def delete(self, model,recursive=True):
        """ 删除系 """
        dept_id = model.get('deptId') if isinstance(model, dict) else model.deptId
        
        if dept_id == 1:
            return IM().set_error("不能删除默认系！")
        
        # 如果已经有专业，就不能删除系
        if (im :=obe.MajorControl.inst().exists({"deptId": dept_id})).error:
            return im
        if im.data:
            return IM().set_error("该系下有课程，不能删除！")
        
        return super().delete(model,recursive)